package com.smec.moblie.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.smec.moblie.R;
import com.smec.moblie.util.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sunxinwei on 16/1/27.
 */
public class MntjobElvAdapter extends BaseExpandableListAdapter {
    private List<String> parent = new ArrayList<String>();
    private Map<String, List<String>> map = new HashMap<String, List<String>>();
    private Context context;

    public MntjobElvAdapter(Context context) {
        this.context = context;
    }

    public void setMntjobElvList(List<String> parent, Map<String, List<String>> map) {
        if (null != parent && null != map) {
            this.parent = parent;
            this.map = map;
            notifyDataSetChanged();
        }
    }

    // 得到子item需要关联的数据
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String key = parent.get(groupPosition);
        return (map.get(key).get(childPosition));
    }

    // 得到子item的ID
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    // 设置子item的组件
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        String key = this.parent.get(groupPosition);
        String info = map.get(key).get(childPosition);
        View view;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.item_mntjobelv, null);
        } else {
            view = convertView;
        }
        TextView tv = (TextView) view.findViewById(R.id.mntjobTv);
        tv.setText(info);
        return view;
    }

    // 获取当前父item下的子item的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        String key = parent.get(groupPosition);
        int size = map.get(key).size();
        return size;
    }

    // 获取当前父item的数据
    @Override
    public Object getGroup(int groupPosition) {
        return parent.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return parent.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // 设置父item组件
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.item_mntjobgroupview, null);
        } else {
            view = convertView;
        }
        TextView tv = (TextView) view.findViewById(R.id.mntjobGroupViewTv);
        tv.setText(Util.conversionStr(this.parent.get(groupPosition)));
        ImageView imageView_icon = (ImageView) view.findViewById(R.id.imageView_icon);

        if (isExpanded) {
            imageView_icon.setImageResource(R.mipmap.image_up);
            view.setSelected(true);
        } else {
            imageView_icon.setImageResource(R.mipmap.image_down);
            view.setSelected(false);
        }

        return view;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}